package com.gxa.service.impl;

import com.gxa.mapper.RoleMapper;
import com.gxa.pojo.PowerMenu;
import com.gxa.pojo.Role;
import com.gxa.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2020-10-30 14:24:00
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired(required = false)
    private RoleMapper roleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    public Role queryById(Integer roleId) {
        return this.roleMapper.queryById(roleId);
    }


    @Override
    public int addRole(Role role) {
        int rows = roleMapper.addRole(role);
        Integer roleId = role.getRoleId();
        return roleId;
    }

    @Override
    public int update(Role role) {
        return roleMapper.update(role);
    }

    @Override
    public int deleteById(Integer roleId) {
        return roleMapper.deleteById(roleId);
    }

    @Override
    public List<Role> queryAll() {
        List<Role> roleList = roleMapper.queryAll();
        for (Role role : roleList) {
            List<PowerMenu> powerMenuList=roleMapper.queryPowerByRoleId(role.getRoleId());
            role.setPowerMenuList(powerMenuList);
        }
        return roleList;
    }

    @Override
    public int addRolePower(int roleId, List<PowerMenu> powerMenuList) {
        List<Map<String,Object>> role_power=roleMapper.selectRolePowerByRoleId(roleId);
        if (role_power!=null){
            roleMapper.deleteRole_powerByRoleId(roleId);
        }
        int result=0;
        for (PowerMenu powerMenu : powerMenuList) {
            result+=roleMapper.addRolePower(roleId,powerMenu.getPowerId());
        }
        return result;
    }

    @Override
    public List<String> selectRoleName() {
        return roleMapper.selectRoleName();
    }
}